package Lee_code;

public class Lee_463 {
    int count;
    public int islandPerimeter(int[][] grid)
    {
        count=0;
        for(int i=0; i<grid.length; i++)
        {
            for(int j=0; j<grid[0].length; j++)
            {
                count_grid(i, j, grid);
            }
        }
        return count;
    }

    private void count_grid(int i, int j, int[][] grid)
    {
        if(grid[i][j]==0)
        {
            //上方向
            if(i>0 && grid[i-1][j]==1)
            {
                count++;
            }
            //下方向
            if(i<grid.length-1 && grid[i+1][j]==1)
            {
                count++;
            }
            //左方向
            if(j>0 && grid[i][j-1]==1)
            {
                count++;
            }

            //右方向
            if(j<grid[0].length-1 && grid[i][j+1]==1)
            {
                count++;
            }
        }
    }


}
